#include <QtGui/QApplication>
#include <QtCore>
#include <QtGui>
#include "qmlapplicationviewer.h"

class EventFilter : public QObject
{
protected:
    bool eventFilter(QObject *obj, QEvent *event)
    {
        QInputContext *ic = qApp->inputContext();
        if (ic)
        {
            if (ic->focusWidget() == 0 && prevFocusWidget)
            {
                QEvent closeSIPEvent(QEvent::CloseSoftwareInputPanel);
                ic->filterEvent(&closeSIPEvent);
            }
            else if (prevFocusWidget == 0 && ic->focusWidget())
            {
                QEvent openSIPEvent(QEvent::RequestSoftwareInputPanel);
                ic->filterEvent(&openSIPEvent);
            }
            prevFocusWidget = ic->focusWidget();
        }
        return QObject::eventFilter(obj,event);
    }

private:
    QWidget *prevFocusWidget;
};

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    EventFilter ef;
    QmlApplicationViewer viewer;
    viewer.installEventFilter(&ef);
//    viewer.engine()
//    viewer.engine().setOfflineStoragePath("/home/user/.aesypig");
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/aesypigmeego/main.qml"));
    viewer.showExpanded();

    return app->exec();
}
